package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.po.Admin;
import com.service.AdminService;

import java.util.List;

@Controller
//@RequestMapping(value = "/admin")
public class AdminController {
	@Autowired
	private AdminService adminService;
	@RequestMapping(value = "/admin/login",method = RequestMethod.POST)
	public String login(@ModelAttribute Admin admin)
	{
	      if(adminService.login(admin)==null)
	      {
	    	 return "redirect:/login.jsp";
	      }
	      return "redirect:/staff/list.action"; 
	    
	}
	@RequestMapping(value = "/admin/list")
	public String list(Model model){
		List <Admin> paging = adminService.List();
		model.addAttribute("List", paging);
	    return  "admin/list";
	}
	@RequestMapping(value = "/admin/save",method = RequestMethod.POST)
	public String save(@ModelAttribute Admin admin){
		if(admin.getId()==0){
	        adminService.add(admin);
		}else
		{
			adminService.update(admin);
		}
	    return "redirect:/admin/list.action";
	}
	@RequestMapping(value = "/admin/edit")
	public String save(@RequestParam(value="id") Integer id,Model model){
		if(id!=0){
		Admin admin = adminService.findById(id);
	    model.addAttribute("admin", admin);
		}
		Admin ad = new Admin ();
		ad.setId(0);
		 model.addAttribute("admin", ad);
		return "admin/detail";
	
	}
	@RequestMapping(value = "/admin/del")
	public String del(@RequestParam(value="id") Integer id){
	    adminService.del(id);
	    return "redirect:/admin/list.action";
	}
}
